home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / ipc / mainpopen.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  701b  |  43 lines

  1. #include    <stdio.h>
  2.  
  3. #define    MAXLINE        1024
  4.  
  5. main()
  6. {
  7.     int    n;
  8.     char    line[MAXLINE], command[MAXLINE + 10];
  9.     FILE    *fp;
  10.  
  11.     /*
  12.      * Read the filename from standard input.
  13.      */
  14.  
  15.     if (fgets(line, MAXLINE, stdin) == NULL)
  16.         err_sys("filename read error");
  17.  
  18.     /*
  19.      * Use popen to create a pipe and execute the command.
  20.      */
  21.  
  22.     sprintf(command, "cat %s", line);
  23.     if ( (fp = popen(command, "r")) == NULL)
  24.         err_sys("popen error");
  25.  
  26.     /*
  27.      * Read the data from the FILE pointer and write
  28.      * to standard output.
  29.      */
  30.  
  31.     while ((fgets(line, MAXLINE, fp)) != NULL) {
  32.         n = strlen(line);
  33.         if (write(1, line, n) != n)
  34.             err_sys("data write error");
  35.     }
  36.  
  37.     if (ferror(fp))
  38.         err_sys("fgets error");
  39.  
  40.     pclose(fp);
  41.     exit(0);
  42. }
  43.